package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.entity.Employee;
import com.itheima.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

/**
 * @Auther: guozy
 * @Date: 2022/7/27 - 07 - 27 - 16:52
 * @Description: com.itheima.controller
 * @version: 1.0
 */
@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    // 自动装配Service 接口
    @Autowired
    private EmployeeService employeeService;

    /**
     * 页面登录操作
     * @param request
     * @param employee
     * @return
     */
    @PostMapping("/login")
    public R<Employee> employeeLogin(HttpServletRequest request, @RequestBody Employee employee){
        QueryWrapper<Employee> eqw = new QueryWrapper<Employee>();
        // 1.判断账号
        eqw.lambda().eq(Employee::getUsername,employee.getUsername());
        Employee emp = employeeService.getOne(eqw);
        // 1.1判断账号是否存在
        if (emp==null){
            return R.error("账号错误");
        }
        // 2.判断密码
        String s = DigestUtils.md5DigestAsHex(employee.getPassword().getBytes());
        if (!emp.getPassword().equals(s)){
            return R.error("密码错误");
        }
        // 3.判断员工状态
        if (emp.getStatus()==0){
            return R.error("员工信息已过期");
        }
        // 4.封装数据并发送
        request.getSession().setAttribute("employee",emp.getId());
        return R.success(emp);
    }

    /**
     * index.html页面右上角退出操作
     * @param request
     * @return
     */
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }


    @PostMapping
    public R<String> addEmployee(HttpServletRequest request,@RequestBody Employee employee){
        // d5加密 设置初始密码
        String pwdMd5 = DigestUtils.md5DigestAsHex("123456".getBytes());
        employee.setPassword(pwdMd5);

       /* // 设置新增员工的时间,和修改员工信息的时间
        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());

        // 获取当前用户的id
        Long e1 = (Long) request.getSession().getAttribute("employee");
        employee.setCreateUser(e1);
        employee.setUpdateUser(e1);*/

        employeeService.save(employee);
        return R.success("成功添加新员工");
    }

    /**
     * 员工信息分页查询
     * @param page page
     * @param pageSize pageSize
     * @param name name
     * @return
     */
    @GetMapping("/page")
    public R<Page> selectPage(int page,int pageSize ,String name){
        log.info("page = {} ,pageSize = {} ,name = {}",page,pageSize,name);

        //构造分页构造器
        Page<Employee> employeePage = new Page<>(page,pageSize);

        // 构造条件过滤器
        LambdaQueryWrapper<Employee> eqw = new LambdaQueryWrapper<>();
        // 添加过滤条件
        eqw.like(StringUtils.isNotEmpty(name),Employee::getName,name);
        // 添加排序条件
        eqw.orderByDesc(Employee::getUpdateTime);
        // 执行查询
        employeeService.page(employeePage,eqw);
        return R.success(employeePage);
    }

    /**
     * 修改信息
     * @param request
     * @param employee
     * @return
     */
    @PutMapping
    public R<String> update(HttpServletRequest request , @RequestBody Employee employee){
        log.info("修改的id={}",employee.getId());

        //从request中获取修改人id
        Long employee1 = (Long) request.getSession().getAttribute("employee");

        /*// 传入参数设置
        employee.setUpdateTime(LocalDateTime.now());
        employee.setUpdateUser(employee1);*/
        employeeService.updateById(employee);

        return R.success("员工信息修改成功");
    }

    /**
     * 根据id查询数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Employee> redateEmployee(@PathVariable Long id){
        log.info("根据员工id回显信息");
        Employee employeeEnd = employeeService.getById(id);
        if (employeeEnd!=null) {
            return R.success(employeeEnd);
        }else {
            return R.error("查无此用户");
        }
    }
}

